package com.example.interviewrecorder.model

import android.os.Parcel
import android.os.Parcelable
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
data class Record(
    var userId:Long,//对应的用户id
    var title: String,//标题
    var content: String,//内容
    var corporateName: String,//公司名称
    var duration: String,//面试时长
    var itvDate:Long,//面试日期
    var rating: Int,//评分
    var createdTime: Long,//创建时间
    var updatedTime: Long//修改时间
) :Parcelable{
    @PrimaryKey(autoGenerate = true)
    var id: Long = 0

    override fun writeToParcel(dest: Parcel, flags: Int) {
        dest.writeLong(id)
        dest.writeLong(userId)
        dest.writeString(title)
        dest.writeString(content)
        dest.writeString(corporateName)
        dest.writeString(duration)
        dest.writeLong(itvDate)
        dest.writeInt(rating)
        dest.writeLong(createdTime)
        dest.writeLong(updatedTime)
    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR:Parcelable.Creator<Record>{
        override fun createFromParcel(source: Parcel): Record {
            val id=source.readLong()
            val userId=source.readLong()
            val title=source.readString()?:""
            val content=source.readString()?:""
            val corporateName=source.readString()?:""
            val duration=source.readString()?:""
            val itvDate=source.readLong()
            val rating=source.readInt()
            val createdTime=source.readLong()
            val updatedTime=source.readLong()
            val record=Record(userId,title,content,corporateName,duration,itvDate,rating,createdTime,updatedTime)
            record.id=id
            return record
        }

        override fun newArray(size: Int): Array<Record?> {
            return arrayOfNulls(size)
        }
    }
}

sealed class Rating private constructor(private var ratingId:Int){
    object ONE_STAR:Rating(RatingId.ABYSMAL)
    object TWO_STAR:Rating(RatingId.SUBPAR)
    object THREE_STAR:Rating(RatingId.SATISFACTORY)
    object FOUR_STAR:Rating(RatingId.PREMIER)
    object FIVE_STAR:Rating(RatingId.PINNACLE)

    fun toRatingId():Int{
        return this.ratingId
    }

    companion object{
        fun getRatingById(id:Int):Rating{
            return when(id){
                RatingId.ABYSMAL->ONE_STAR
                RatingId.SUBPAR->TWO_STAR
                RatingId.SATISFACTORY->THREE_STAR
                RatingId.PREMIER->FOUR_STAR
                RatingId.PINNACLE->FIVE_STAR
                else->FIVE_STAR
            }
        }
    }

    internal object RatingId{
        //糟糕透顶
        const val ABYSMAL=1

        //低于标准
        const val SUBPAR=2

        //满意的
        const val SATISFACTORY=3

        //卓越的
        const val PREMIER=4

        //顶峰
        const val PINNACLE=5
    }
}